const db = require("../db");

// message_title,
// message_category,
// message_publish_department,
// message_publish_name,
// message_receipt_object,
// message_content,
// message_level,
// message_click_number,
// message_create_time,
// message_status
// message_update_time,

const messageController = {
  // 发布消息
  publishMessage: async (req, res) => {
    const {
      message_title,
      message_category,
      message_publish_department,
      message_publish_name,
      message_receipt_object,
      message_content,
      message_level,
    } = req.body;
    const message_create_time = new Date();
    const sql = "INSERT INTO message SET ?";
    try {
      const [data] = await db.query(sql, {
        message_title,
        message_category,
        message_publish_department,
        message_publish_name,
        message_receipt_object,
        message_content,
        message_level,
        message_create_time,
        message_click_number: 0,
        message_status: 1,
      });
      res.send({
        status: 1,
        message: "成功",
        data: data.insertId,
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 获取消息列表
  getMessageList: async (req, res) => {
    const { page, message_category } = req.query;
    const sql = `SELECT * FROM message WHERE message_category = ? AND message_status = '1' ORDER BY message_create_time DESC LIMIT 10 OFFSET ${
      (page - 1) * 10
    }`;
    const sql1 = `SELECT * FROM message WHERE message_category = ? AND message_status = '1'`;
    try {
      const [data] = await db.query(sql, message_category);
      const [total] = await db.query(sql1, message_category);
      res.send({
        status: 1,
        message: "成功",
        data: data,
        total: total.length,
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 编辑消息
  editMessage: async (req, res) => {
    const {
      message_title,
      message_content,
      message_receipt_object,
      message_level,
      id,
    } = req.body;
    const getOldDep = async (id) => {
      const sql = "SELECT message_receipt_object FROM message WHERE id = ?";
      const [data] = await db.query(sql, id);
      return data[0].message_receipt_object;
    };
    const oldId = await getOldDep(id);
    const message_update_time = new Date();
    const sql =
      "UPDATE message SET message_title = ?, message_content = ?,message_receipt_object = ?,message_level = ?, message_update_time =? WHERE id = ?";
    try {
      await db.query(sql, [
        message_title,
        message_content,
        message_receipt_object,
        message_level,
        message_update_time,
        id,
      ]);
      res.send({
        status: 1,
        message: "成功",
        data: oldId,
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 根据发布部门筛选消息
  searchMessageByDepartment: async (req, res) => {
    const { page, message_publish_department } = req.query;
    const sql = `SELECT * FROM message WHERE message_status = '1' AND message_category = '通知公告' AND message_publish_department = ?  ORDER BY message_create_time DESC LIMIT 10 OFFSET ${
      (page - 1) * 10
    }`;
    const sql1 = `SELECT * FROM message WHERE message_status = '1' AND message_category = '通知公告' AND message_publish_department = ?`;
    try {
      const [data] = await db.query(sql, message_publish_department);
      const [total] = await db.query(sql1, message_publish_department);
      res.send({
        status: 1,
        message: "成功",
        data: data,
        total: total.length,
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 根据发布等级筛选消息
  searchMessageByLevel: async (req, res) => {
    const { page, message_level } = req.query;
    const sql = `SELECT * FROM message WHERE message_status = '1' AND message_category = '通知公告' AND message_level = ?  ORDER BY message_create_time DESC LIMIT 10 OFFSET ${
      (page - 1) * 10
    }`;
    const sql1 = `SELECT * FROM message WHERE message_status = '1' AND message_category = '通知公告' AND message_level = ?`;
    try {
      const [data] = await db.query(sql, message_level);
      const [total] = await db.query(sql1, message_level);
      res.send({
        status: 1,
        message: "成功",
        data: data,
        total: total.length,
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 多条件筛选
  searchMessage: async (req, res) => {
    const { page, message_publish_department, message_level } = req.query;
    const sql = `SELECT * FROM message WHERE message_status = '1' AND message_category = '通知公告' AND message_publish_department = ? AND message_level = ? ORDER BY message_create_time DESC LIMIT 10 OFFSET ${
      (page - 1) * 10
    }`;
    const sql1 = `SELECT * FROM message WHERE message_status = '1' AND message_category = '通知公告' AND message_publish_department = ? AND message_level = ?`;
    try {
      const [data] = await db.query(sql, [
        message_publish_department,
        message_level,
      ]);
      const [total] = await db.query(sql1, [
        message_publish_department,
        message_level,
      ]);
      res.send({
        status: 1,
        message: "成功",
        data: data,
        total: total.length,
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 获取详细信息
  getMessage: async (req, res) => {
    const { id } = req.query;
    const sql = `SELECT * FROM message WHERE id = ?`;
    try {
      const [data] = await db.query(sql, id);
      res.send({
        status: 1,
        message: "成功",
        data: data,
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 更新点击率
  updateClick: async (req, res) => {
    const { message_click_number, id } = req.body;
    const number = message_click_number * 1 + 1;
    const sql = `UPDATE message SET message_click_number = ? WHERE id = ?`;
    try {
      await db.query(sql, [number, id]);
      res.send({
        status: 1,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 放入回收站
  firstDelete: async (req, res) => {
    const message_status = 0;
    const message_delete_time = new Date();
    const { id } = req.body;
    const sql = `UPDATE message SET message_status = ?,message_delete_time = ?  WHERE id = ?`;
    try {
      await db.query(sql, [message_status, message_delete_time, id]);
      res.send({
        status: 1,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 获取回收站列表
  getRecycliList: async (req, res) => {
    const { page } = req.query;
    const sql = `SELECT * FROM message WHERE message_status = '0' LIMIT 10 OFFSET ${
      (page - 1) * 10
    }`;
    const sql1 = `SELECT * FROM message WHERE message_status = '0'`;
    try {
      const [data] = await db.query(sql);
      const [total] = await db.query(sql1);
      res.send({
        status: 1,
        message: "成功",
        data: data,
        total: total.length,
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 还原
  recover: async (req, res) => {
    const message_status = 1;
    const message_update_time = new Date();
    const { id } = req.body;
    const sql = `UPDATE message SET message_status = ?,message_update_time = ?, message_delete_time = NULL WHERE id = ?`;
    try {
      await db.query(sql, [message_status, message_update_time, id]);
      res.send({
        status: 1,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 彻底删除
  deleteMessage: async (req, res) => {
    const { id } = req.body;
    const sql = `DELETE FROM message WHERE id = ?`;
    try {
      await db.query(sql, id);
      res.send({
        status: 1,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
};

module.exports = messageController;
